#include "cjson/cjson.h"
#include "log/log.h"

int main()
{
    //1.打印json字符串
    //先模拟一个字符串
    char *name = "zhangsan";
    int age = 18;
    //先创建一个json对象
    cJSON *root = cJSON_CreateObject();
    //添加键值对
    cJSON_AddStringToObject(root, "name", name);
    cJSON_AddNumberToObject(root, "age", age);
    //将json对象转换为字符串
    char *json_str = cJSON_Print(root);
    //打印json字符串
    log_debug("json_str:%s", json_str);
    //解析json字符串
    cJSON *json = cJSON_Parse(json_str);
    //获取json中的值
    cJSON *name_obj = cJSON_GetObjectItem(root, "name");
    cJSON *age_obj = cJSON_GetObjectItem(root, "age");
    if (name_obj == NULL && age_obj == NULL)
    {
        log_error("解析错误");
        return -1;
    }
    //赋值
    char *name1 = name_obj->valuestring;
    int age1 = age_obj->valueint;
    log_debug("name:%s, age:%d", name1, age1);
    //释放内存
    cJSON_Delete(root);
    cJSON_Delete(json); 
    return 0;

    
}